package com.lanou.musicproject.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.Constant;
import com.lanou.musicproject.bean.MusicResult;
import com.lanou.musicproject.fragment.SingerDescribFragment;
import com.lanou.musicproject.fragment.SingerSongFragment;
import com.lanou.musicproject.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/26.
 * 歌曲歌手信息页面
 */
public class SingerInfoActitity extends AppCompatActivity implements GestureDetector.OnGestureListener, View.OnTouchListener  {

    private CoordinatorLayout rootView;
    private GestureDetector mGesture;
    private MusicResult musicResult;// 跳转传过来的值
    private String pic_url; // 歌手图片
    private String singer_name;// 歌手名
    private ImageView topIv; // 标题栏头像
    private CollapsingToolbarLayout topTv; // 标题栏标题

    private long singer_id;// 传给fragment,拼接网址,解析用
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private MyPagerAdapter pagerAdapter;
    private ArrayList<Fragment> datas;

    private SingerSongFragment songFragment;
    private SingerDescribFragment describFragment;

    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singerinfo);

        Intent intentBack = getIntent();
        if (intentBack != null) {
            musicResult = intentBack.getParcelableExtra("result");
            pic_url = musicResult.getPic_url();
            singer_name = musicResult.getSinger_name();
            singer_id = musicResult.getSinger_id();
        }
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        rootView = (CoordinatorLayout) findViewById(R.id.singer_root_view);
        // 手势
        rootView.setOnTouchListener(this);
        mGesture = new GestureDetector(this);
        volleySingleton = VolleySingleton.getInstance(this);
        imageLoader = volleySingleton.getImageLoader();
        // 初始化标题栏并赋值
        topIv = (ImageView) findViewById(R.id.singer_topbar_iv);
        topTv = (CollapsingToolbarLayout) findViewById(R.id.singer_cool_title);
        topTv.setTitle(singer_name);
        Picasso.with(this).load(pic_url).into(topIv);
        mViewPager = (ViewPager) findViewById(R.id.singer_viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.singer_tablayout);

        // 初始化viewpager的fragment
        // 将singer_id传给fragment
        songFragment = new SingerSongFragment();
        describFragment = new SingerDescribFragment();

        Bundle bundle = new Bundle();
        bundle.putLong("singer", singer_id);

        songFragment.setArguments(bundle);

        describFragment.setArguments(bundle);

        datas = new ArrayList<>();
        datas.add(songFragment);
        datas.add(describFragment);
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), datas);
        mViewPager.setAdapter(pagerAdapter);

        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.base));
        mTabLayout.setTabTextColors(getResources().getColor(R.color.black_color), getResources().getColor(R.color.base));
        mTabLayout.setupWithViewPager(mViewPager);

    }

    /**
     * 手势
     * 左滑退出
     */
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e2.getX() - e1.getX() > Constant.MINDISTANCE && Math.abs(velocityX) > Constant.MINVELOCITY && (Math.abs(e2.getY() - e1.getY()) < Constant.MIDDLEDISTANCE)) {
            finish();
        }
        else if (e1.getX() - e2.getX() > Constant.MIDDLEDISTANCE && Math.abs(velocityX) > Constant.MINVELOCITY && (Math.abs(e2.getY() - e1.getY())) < Constant.MIDDLEDISTANCE) {
            Toast.makeText(SingerInfoActitity.this, "左滑", Toast.LENGTH_SHORT).show();
        }

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGesture.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mGesture.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 内部类适配器
     */
    private class MyPagerAdapter extends FragmentPagerAdapter {

        private ArrayList<Fragment> d;
        private String[] tabName = {"歌曲信息", "歌手简介"};

        public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> d) {
            super(fm);
            this.d = d;
        }

        @Override
        public Fragment getItem(int position) {
            return d.get(position);
        }

        @Override
        public int getCount() {
            return d.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabName[position];
        }
    }
}
